﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using XYS.Service.Core.Models;
using XYS.Service.Core.Service;

namespace XYS.Service.Core
{
    /// <summary>
    /// 缓存服务 相关配置
    /// </summary>
    public static class OSSExtension
    {
        /// <summary>
        /// 增加 对象存储
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public static WebApplicationBuilder AddOSS(this WebApplicationBuilder builder, ILogger logger)
        {
            var ossSettings = builder.Configuration.GetSection("OSSSettings").Get<List<OSSSetting>>();
            if (ossSettings != null && ossSettings.Count > 0)
            {
                builder.Services.AddSingleton(s => ossSettings);
                var ossSetting = ossSettings.FirstOrDefault(x => x.Enable);

                if (ossSetting != null)
                {
                    switch (ossSetting.OSSType.ToLower())
                    {
                        case "aliyun":
                            logger.LogInformation($"添加对象存储[{ossSetting.OSSType.ToLower()}]");
                            builder.Services.AddSingleton<IOSSService, AliyunOSSService>();
                            break;
                        default:
                            logger.LogWarning($"对象存储[{ossSetting.OSSType.ToLower()}]尚未实现");
                            break;
                    }

                }
            }
            return builder;
        }

    }
}